/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Login;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Bharath
 */
public class ChangePassword extends HttpServlet {

    private String query;
    private PreparedStatement pstmt;
    private int rs;
    MySqlConnector ms = new MySqlConnector();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            System.out.println("*********&&&&**************");
            HttpSession hs = request.getSession(true);
            String memberid = hs.getAttribute("memid").toString();
            String sesspass = hs.getAttribute("password").toString();
            String oldpass = request.getParameter("oldpass");
            String password = request.getParameter("password");
            String password1 = request.getParameter("password1");
            System.out.println("member id is ......." + memberid);
            System.out.println("old password is ......." + oldpass);
            System.out.println(" New Password is ......." + password1);

            if(oldpass.equals(sesspass))
            {
             System.out.print("Two passwords in if condition "+password);
            ms.Connect();
            query = "update registration set password = '"+password1+"' where memberid = '"+memberid+"' and password = '"+oldpass+"'  ; ";
            //Statement
            pstmt = ms.con.prepareStatement(query);
            //Result Set
            rs = pstmt.executeUpdate();
            System.out.println("integer value . . .. . .  "+rs);
            //set sessions
            if( rs == 1)
            {
                out.println("<script> alert('Successful Entry');<script>");
                getServletContext().getRequestDispatcher("/whos.jsp").forward(request, response);
                hs.setAttribute("cpass", password1);
            }
            else
            {
                //out.println("<script> alert('Invalid Entry');<script>");
                getServletContext().getRequestDispatcher("ChangePass.jsp").forward(request, response);
            }
            }
        } catch (Exception ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }

    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
